package com.redpack.redpost.struts.action.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.redpack.redpost.struts.form.admin.multimediaForm;
import com.redpack.redpost.util.dao.adminDAO;
import com.redpack.redpost.util.to.generalUtil;
import com.redpack.redpost.util.to.multimediaTo;

public class editarmultimedia extends Action {
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		boolean logginVal = generalUtil.isLoggedInAdmin(request);
		if(logginVal!=true){
			return mapping.findForward("noLogin") ;
		}
		
		String filtro = "0", texto = "";
		if( request.getParameter("filtro") != null ){
			filtro = request.getParameter("filtro");
		}
		
		if( request.getParameter("texto") != null ){
			texto = request.getParameter("texto");
		}
		
		multimediaForm f = (multimediaForm)form;
		
		if( f != null ){
			if( f.getAcc().equals("2") ){
				adminDAO.eliMultimedia(f.getCve_multimedia());
			}
			
			if( f.getAcc().equals("1") ){
				List slides = new ArrayList();
				List teasers = new ArrayList();
				try {
					slides = adminDAO.getSlides();
					teasers = adminDAO.getTeaser();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.setAttribute("slides", slides);
				request.setAttribute("teasers", teasers);
		
				multimediaTo multimedia_info = adminDAO.getMultimedia( f.getCve_multimedia() );
				
				request.setAttribute("nombre_sel", multimedia_info.getNombre());
				request.setAttribute("cve_multimedia", ""+multimedia_info.getCve_multimedia());
				request.setAttribute("teaser_sel", ""+multimedia_info.getTeaser());
				request.setAttribute("slide_sel", ""+multimedia_info.getSlide());
				request.setAttribute("link_sel", ""+multimedia_info.getLink());
				request.setAttribute("text_sel", ""+multimedia_info.getText());
				request.setAttribute("orden_sel", ""+multimedia_info.getOrden());
				
				return mapping.findForward("edit") ;
			}
		}
		
		request.setAttribute("filtro", filtro);
		List multimedia = adminDAO.getMultimedia( filtro , texto, 1 );
		request.setAttribute("multimedia", multimedia);
		
		return mapping.findForward("success") ;
	}

}